using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class CustomControl : UserControl
{
	private int selectedNumber = -1;

	private Label label;

	private HashSet<int> SelectMass = new HashSet<int>();

	private bool flag = false;

	private int _value = 0;

	private IContainer components = null;

	public HashSet<int> SelectMassList
	{
		get
		{
			return SelectMass;
		}
		set
		{
			SelectMass = value;
		}
	}

	[Description("当前值")]
	[Category("Parameters")]
	public int Value
	{
		get
		{
			return _value;
		}
		set
		{
			_value = value;
		}
	}

	public event EventHandler ValueChanged;

	public CustomControl()
	{
		InitializeComponent();
		SetStyle(ControlStyles.OptimizedDoubleBuffer, value: true);
		SetStyle(ControlStyles.AllPaintingInWmPaint, value: true);
		SetStyle(ControlStyles.UserPaint, value: true);
		label = new Label();
		label.AutoSize = true;
		label.Location = new Point(10, 10);
		label.Text = "质量数：---";
		base.Controls.Add(label);
		base.MouseClick += CustomControl_MouseClick;
	}

	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		Graphics g = e.Graphics;
		int rectWidth = base.Width / 100;
		for (int j = 0; j < 99; j++)
		{
			Rectangle rect = new Rectangle(j * rectWidth, 50, rectWidth, 50);
			if (j + 2 == selectedNumber && flag)
			{
				g.DrawRectangle(Pens.Black, rect);
				SelectMass.Remove(selectedNumber);
				continue;
			}
			if (j + 2 == selectedNumber)
			{
				g.FillRectangle(Brushes.Red, rect);
				if (!flag)
				{
					SelectMass.Add(selectedNumber);
				}
			}
			else
			{
				g.DrawRectangle(Pens.Black, rect);
			}
			if (SelectMass.Contains(j + 2))
			{
				g.FillRectangle(Brushes.Red, rect);
			}
		}
		for (int l = 1; l <= 10; l++)
		{
			int x3 = l * 10 * rectWidth;
			g.DrawLine(Pens.Black, x3 - 12, 100, x3 - 12, 110);
			g.DrawString((l * 10).ToString(), Font, Brushes.Black, x3 - 17, 120f);
		}
		for (int n = 0; n < 99; n++)
		{
			Rectangle rect3 = new Rectangle(n * rectWidth, 170, rectWidth, 50);
			if (n + 101 == selectedNumber && flag)
			{
				g.DrawRectangle(Pens.Black, rect3);
				SelectMass.Remove(selectedNumber);
				continue;
			}
			if (n + 101 == selectedNumber)
			{
				g.FillRectangle(Brushes.Red, rect3);
				if (!flag)
				{
					SelectMass.Add(selectedNumber);
				}
			}
			else
			{
				g.DrawRectangle(Pens.Black, rect3);
			}
			if (SelectMass.Contains(n + 101))
			{
				g.FillRectangle(Brushes.Red, rect3);
			}
		}
		for (int m = 1; m <= 10; m++)
		{
			int x2 = m * 10 * rectWidth;
			g.DrawLine(Pens.Black, x2 - 12, 220, x2 - 12, 230);
			g.DrawString(((m + 10) * 10).ToString(), Font, Brushes.Black, x2 - 16, 240f);
		}
		for (int k = 0; k < 59; k++)
		{
			Rectangle rect2 = new Rectangle(k * rectWidth, 290, rectWidth, 50);
			if (k + 201 == selectedNumber && flag)
			{
				g.DrawRectangle(Pens.Black, rect2);
				SelectMass.Remove(selectedNumber);
				continue;
			}
			if (k + 201 == selectedNumber)
			{
				g.FillRectangle(Brushes.Red, rect2);
				if (!flag)
				{
					SelectMass.Add(selectedNumber);
				}
			}
			else
			{
				g.DrawRectangle(Pens.Black, rect2);
			}
			if (SelectMass.Contains(k + 201))
			{
				g.FillRectangle(Brushes.Red, rect2);
			}
		}
		for (int i = 1; i <= 6; i++)
		{
			int x = i * 10 * rectWidth;
			g.DrawLine(Pens.Black, x - 12, 340, x - 12, 350);
			g.DrawString(((i + 20) * 10).ToString(), Font, Brushes.Black, x - 16, 360f);
		}
	}

	private void CustomControl_MouseClick(object sender, MouseEventArgs e)
	{
		int rectWidth = base.Width / 100;
		int rectHeight = 50;
		if (e.Y >= 50 && e.Y <= 100)
		{
			int index = e.X / rectWidth;
			selectedNumber = index + 2;
		}
		else if (e.Y >= 170 && e.Y <= 220)
		{
			int index2 = e.X / rectWidth;
			selectedNumber = index2 + 101;
		}
		else if (e.Y >= 290 && e.Y <= 340)
		{
			int index3 = e.X / rectWidth;
			selectedNumber = index3 + 201;
		}
		else
		{
			selectedNumber = -1;
		}
		if (selectedNumber != -1)
		{
			label.Text = "质量数：" + selectedNumber;
			if (SelectMass.Contains(selectedNumber))
			{
				flag = true;
			}
			else
			{
				flag = false;
			}
			_value = selectedNumber;
			if (this.ValueChanged != null)
			{
				this.ValueChanged(this, null);
			}
		}
		else
		{
			label.Text = "质量数：---";
		}
		Invalidate();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	}
}
